home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 February: Tool Chest / Dev.CD Feb 94.toast / Tool Chest / Development Platforms / AppsToGo / AppsToGo.src / DTS.Draw / Clipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-18  |  7.1 KB  |  233 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        Clipboard.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19. /* This file contains the code for the document procedure pointers for the DTS.Draw
  20. ** clipboard document.  The clipboard document is simply a modified DTS.Draw document.
  21. ** Many of the main document facilities are removed, since they don't apply to the
  22. ** clipboard.  See ClipboardInitDocument() for a full breakdown of the changes in
  23. ** the document procedures. */
  24.  
  25.  
  26.  
  27. /*****************************************************************************/
  28.  
  29.  
  30.  
  31. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  32. #include "App.defs.h"        /* Get various application definitions.            */
  33. #include "App.protos.h"        /* Get the prototypes for the application.        */
  34.  
  35. #ifndef __ERRORS__
  36. #include <Errors.h>
  37. #endif
  38.  
  39. #ifndef __TREEOBJ2__
  40. #include "TreeObj2.h"
  41. #endif
  42.  
  43.  
  44.  
  45. /*****************************************************************************/
  46.  
  47.  
  48.  
  49. extern RgnHandle    gCursorRgn;
  50. extern CursPtr        gCursorPtr;
  51.  
  52. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window);
  53. static void        ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick);
  54. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough);
  55. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt);
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60. /*****************************************************************************/
  61.  
  62.  
  63.  
  64. /* Initialize the clipboard document. */
  65.  
  66. #pragma segment ClipboardDoc
  67. OSErr    ClipboardInitDocument(FileRecHndl frHndl)
  68. {
  69.     OSErr        err;
  70.     FileRecPtr    frPtr;
  71.  
  72.     err = DefaultInitDocument(frHndl, 0, 0, 0);
  73.         /* Use the hierarchical document architecture. */
  74.  
  75.     if (!err) {        /* Set some custom window behaviors, and disable the rest. */
  76.         frPtr = *frHndl;
  77.         frPtr->fileState.calcFrameRgnProc        = nil;
  78.         frPtr->fileState.contentClickProc        = ClipboardContentClick;
  79.         frPtr->fileState.contentKeyProc          = ClipboardContentKey;
  80.         frPtr->fileState.drawFrameProc           = nil;
  81.         frPtr->fileState.freeDocumentProc        = nil;
  82.         frPtr->fileState.freeWindowProc          = nil;
  83.         frPtr->fileState.initContentProc         = ClipboardInitContent;
  84.         frPtr->fileState.readDocumentProc        = nil;
  85.         frPtr->fileState.readDocumentHeaderProc  = nil;
  86.         frPtr->fileState.resizeContentProc       = nil;
  87.         frPtr->fileState.scrollFrameProc         = nil;
  88.         frPtr->fileState.undoFixupProc           = nil;
  89.         frPtr->fileState.windowCursorProc        = ClipboardWindowCursor;
  90.         frPtr->fileState.writeDocumentProc       = nil;
  91.         frPtr->fileState.writeDocumentHeaderProc = nil;
  92.     }
  93.  
  94.     return(err);
  95. }
  96.  
  97.  
  98.  
  99. /*****************************************************************************/
  100.  
  101.  
  102.  
  103. /* Initialize the clipboard document size.  By waiting this late to state the
  104. ** document size, the window is initially opened to the size described in the
  105. ** 'WIND' resource for the clipboard.  Once the window exists, we can then
  106. ** set the document size to be 7 inches by 10 inches. */
  107.  
  108. #pragma segment ClipboardDoc
  109. static OSErr    ClipboardInitContent(FileRecHndl frHndl, WindowPtr window)
  110. {
  111. #pragma unused (window)
  112.  
  113.     SetDocSize(frHndl, (7 * 72), (10 * 72));
  114.     return(noErr);
  115. }
  116.  
  117.  
  118.  
  119. /*****************************************************************************/
  120. /*****************************************************************************/
  121.  
  122.  
  123.  
  124. /* Handle only document scrolling for the clipboard. */
  125.  
  126. #pragma segment ClipboardDoc
  127. static void    ClipboardContentClick(WindowPtr window, EventRecord *event, Boolean firstClick)
  128. {
  129. #pragma unused (frHndl, firstClick)
  130.  
  131.     IsCtlEvent(window, event, nil, nil);
  132. }
  133.  
  134.  
  135.  
  136. /*****************************************************************************/
  137.  
  138.  
  139.  
  140. /* No keys for the clipboard.  Returning true eats all of the keys, so that
  141. ** they aren't passed on to the next window behind the clipboard. */
  142.  
  143. #pragma segment ClipboardDoc
  144. static Boolean    ClipboardContentKey(WindowPtr window, EventRecord *event, Boolean *passThrough)
  145. {
  146. #pragma unused (window, event, passThrough)
  147.  
  148.     return(true);
  149. }
  150.  
  151.  
  152.  
  153. /*****************************************************************************/
  154.  
  155.  
  156.  
  157. /* Whenever the clipboard is active, just use an arrow cursor. */
  158.  
  159. #pragma segment ClipboardDoc
  160. static Boolean    ClipboardWindowCursor(FileRecHndl frHndl, WindowPtr window, Point globalPt)
  161. {
  162. #pragma unused (frHndl, window, globalPt)
  163.  
  164.     SetCursor(gCursorPtr = &qd.arrow);
  165.     return(true);
  166. }
  167.  
  168.  
  169.  
  170. /*****************************************************************************/
  171. /*****************************************************************************/
  172.  
  173.  
  174.  
  175. /* Handle cut/copy/paste for the clipboard. */
  176.  
  177. #pragma segment ClipboardDoc
  178. void    DoClipboard(FileRecHndl frHndl, short menuItem)
  179. {
  180.     FileRecHndl    frClip;
  181.     TreeObjHndl    root, rootClip, chndl;
  182.     short        cnum;
  183.     WindowPtr    clipWind, window;
  184.  
  185.     if (!(clipWind = GetNextWindow(nil, kClipboardFileType))) return;
  186.  
  187.     frClip   = (FileRecHndl)GetWRefCon(clipWind);
  188.     rootClip = (*frClip)->d.doc.root;
  189.     root     = (*frHndl)->d.doc.root;
  190.  
  191.     if (menuItem != kStdCopy)
  192.         NewDocumentUndo(frHndl);
  193.  
  194.     switch (menuItem) {
  195.         case kStdCut:
  196.         case kStdCopy:
  197.             while ((*rootClip)->numChildren) DisposeChild(NO_EDIT, rootClip, 0);
  198.                 /* First dispose of the clipboard's old content. */
  199.             for (cnum = (*root)->numChildren; cnum;) {
  200.                 chndl = GetChildHndl(root, --cnum);
  201.                 if (mDerefCommon(chndl)->selected) {    /* If document object is selected...    */
  202.                     CopyChild(NO_EDIT, root, cnum, rootClip, 0, true);            /* Copy it.     */
  203.                     mDerefCommon(GetChildHndl(rootClip, 0))->selected = false;    /* Deselect it. */
  204.                     mDerefRoot(rootClip)->numSelected = 0;
  205.                         /* Copying the child caused it to get selected, and to increment the
  206.                         ** numSelected value in the clipboard's root.  Reset it. */
  207.                 }
  208.             }
  209.             if (((WindowPeek)clipWind)->visible) {        /* If clipboard visible... */
  210.                 BeginContent(clipWind);        /* Redraw clipboard to show the new content. */
  211.                 DoImageDocument(frClip);
  212.                 EndContent(clipWind);
  213.             }
  214.             if (menuItem == kStdCut)    /* If clipboard operation was a delete... */
  215.                 DoDelete(frHndl);        /* Delete the objects out of the document. */
  216.             break;
  217.         case kStdPaste:
  218.             DoTreeSelect(root, SELECTOFF);        /* Turn off current document selections. */
  219.             for (cnum = (*rootClip)->numChildren; cnum;) {
  220.                 chndl = GetChildHndl(rootClip, --cnum);
  221.                 CopyChild(CLIPBOARD_EDIT, rootClip, cnum, root, 0, true);
  222.                     /* Copy the clipboard objects with deepCopy to copy grouped objects as well. */
  223.             }
  224.             BeginContent(window = (*frHndl)->fileState.window);        /* Redraw document. */
  225.             DoImageDocument(frHndl);
  226.             EndContent(window);
  227.             break;
  228.     }
  229. }
  230.  
  231.  
  232.  
  233.